const { merge } = require('webpack-merge')
const { SERVER_HOST, SERVER_PORT, PROJECT_PATH } = require('../constants')
const common = require('./webpack.common.js')
const { resolve } = require('path')
const webpack = require('webpack')
const proxySettings = require('../../src/setProxy')

module.exports = merge(common, {
  mode: 'development',
  target: 'web',
  output: {
    // 开发环境不需要添加hash 生产环境必须添加 防止缓存
    filename: `js/[name]'.js`,
    path: resolve(PROJECT_PATH, './dist'),
  },
  stats: 'errors-only', // 终端仅打印 error
  devtool: 'eval-source-map',
  devServer: {
    static: resolve(PROJECT_PATH, 'dist'), //本地服务器所加载的页面所在的目录
    historyApiFallback: true, // 不跳转
    host: '127.0.0.1',
    port: 7000,
    // hot: true,  配置了 HMR 会自动开启
    compress: true, //Enable gzip compression for everything served
    client: {
      overlay: true, //Shows a full-screen overlay in the browser
    },
    open: true, //When open is enabled, the dev server will open the browser.
    // 配置代理
    proxy: {
      ...proxySettings,
      '/api': {
        target: 'http://127.0.0.1:7001',
        changeOrigin: true,
      },
    },
  },
  plugins: [new webpack.HotModuleReplacementPlugin()],
})
